package com.imay.live.fragment;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.imay.base.fragment.BaseFragment;
import com.imay.base.util.StringUtil;
import com.imay.live.activity.HelpDetailActivity;
import com.imay.live.activity.WebViewActivity;
import com.imay.live.adapter.HelpAdapter;
import com.imay.live.model.HelpModel;
import com.imay.live.utils.RecyclerViewUtil;
import com.imay.live.R;

import java.io.Serializable;
import java.util.List;

/**
 * created by nick
 * 帮助的tab
 */

public class HelpTabFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private RecyclerView rcv_help;
    private List<HelpModel.HelpItemModel> helpItemModels;
    private String title;


    public static HelpTabFragment newInstance(List<HelpModel.HelpItemModel> param1,String title) {
        HelpTabFragment fragment = new HelpTabFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, (Serializable) param1);
        args.putString(ARG_PARAM2, title);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            helpItemModels = (List<HelpModel.HelpItemModel>) getArguments().getSerializable(ARG_PARAM1);
            title =  getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_help_tab;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rcv_help = $(R.id.rcv_help);
        RecyclerViewUtil.setLinearLayoutManager(getActivity(),rcv_help);
        bindAdapter();

    }
    private void bindAdapter(){
        HelpAdapter helpAdapter = new HelpAdapter(helpItemModels);
        helpAdapter.setOnItemClickListener(this);
        rcv_help.setAdapter(helpAdapter);

    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        HelpModel.HelpItemModel model =helpItemModels.get(position);
        if (!StringUtil.isEmpty(model.getUrl())){
            WebViewActivity.startActivity(getActivity(),model.getUrl());
            return;
        }
        HelpDetailActivity.start(getActivity(),model,title);

    }
}
